package com.dap.configuration.service.config.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baidu.disconf.core.common.constants.DisConfigTypeEnum;
import com.baidu.disconf.web.service.config.bo.Config;
import com.baidu.dsp.common.dao.AbstractDao;
import com.baidu.dsp.common.dao.Columns;
import com.baidu.dsp.common.form.RequestListBase.Page;
import com.baidu.dsp.common.utils.DaoUtils;
import com.baidu.ub.common.db.DaoPage;
import com.baidu.ub.common.db.DaoPageResult;
import com.baidu.unbiz.common.genericdao.operator.Match;
import com.baidu.unbiz.common.genericdao.operator.Order;
import com.baidu.unbiz.common.genericdao.param.BetweenParam;
import com.baidu.unbiz.common.genericdao.param.GteParam;
import com.baidu.unbiz.common.genericdao.param.LteParam;
import com.dap.configuration.service.config.dao.ConfigExtensionDao;

/**
 * @author dengxiang
 * @version 2014-6-16
 */
@Service
public class ConfigExtensionDaoImpl extends AbstractDao<Long, Config> implements ConfigExtensionDao {

    /**
     *
     */
    @Override
    public List<Config> getConfByAppEnv(Long appId, Long envId) {

        if (envId == null) {
            return find(new Match(Columns.APP_ID, appId));
        } else if(appId == null){
        	return find(new Match(Columns.ENV_ID, envId));
        }else{
        	return find(new Match(Columns.APP_ID, appId), new Match(Columns.ENV_ID, envId));
        }
    }

    /**
     *
     */
    @Override
    public DaoPageResult<Config> getConfigList(Long appId, Long envId, String version, String bankKey ,String startTime,String endTime,String userId ,String name, Page page) {

        DaoPage daoPage = DaoUtils.daoPageAdapter(page);
        List<Match> matchs = new ArrayList<Match>();

        matchs.add(new Match(Columns.APP_ID, appId));

        matchs.add(new Match(Columns.ENV_ID, envId));

        matchs.add(new Match(Columns.VERSION, version));
        
        matchs.add(new Match("bank_key", bankKey));
        
        if(!StringUtils.isEmpty(startTime)){
        	matchs.add(new Match(Columns.UPDATE_TIME, new GteParam(startTime)));
        }
        if(!StringUtils.isEmpty(endTime)){
        	matchs.add(new Match(Columns.UPDATE_TIME, new LteParam(endTime)));
        }
        if(!StringUtils.isEmpty(name)){
        	matchs.add(new Match(Columns.NAME, name));
        }

        return page2(matchs, daoPage);
    }

    /**
    *
    */
   @Override
   public List<Config> getConfigList(Long appId, Long envId, String version,String bankKey,String userId) {

       List<Match> matchs = new ArrayList<Match>();

       matchs.add(new Match(Columns.APP_ID, appId));

       matchs.add(new Match(Columns.ENV_ID, envId));

       matchs.add(new Match(Columns.VERSION, version));
       
       matchs.add(new Match("bank_key", bankKey));

       return find(matchs, new ArrayList<Order>());
   }


	@Override
	public Config getByParameter(Long appId, Long envId, String version,
			String key, DisConfigTypeEnum disConfigTypeEnum, String bankKey,
			String userId) {
		return findOne(new Match(Columns.APP_ID, appId), new Match(Columns.ENV_ID, envId),
                new Match(Columns.VERSION, version), new Match(Columns.TYPE, disConfigTypeEnum.getType()),
                new Match(Columns.NAME, key),new Match("bank_key", bankKey));
	}

	@Override
	public List<Config> getConfigList(String bankKey, String type) {
	       List<Match> matchs = new ArrayList<Match>();

	       matchs.add(new Match("bank_key", bankKey));

	       matchs.add(new Match(Columns.TYPE, type));

	       return find(matchs, new ArrayList<Order>());
	}

}
